﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using DaInterFace;
using DaInterFace.Mobile;

namespace DaFunction.Mobile
{
    public class MobileClientMessage : IClientMessage
    {
        public event MobileClientMessageChange MessageChanged;
        //private Dictionary<String, ClientMessageStruct> _msg = new Dictionary<String, ClientMessageStruct>();
        private Dictionary<String, List<String>> _userMap = new Dictionary<String, List<String>>();
        private Dictionary<String, MobSrvMessageStruct> _messages = new Dictionary<String, MobSrvMessageStruct>();

        public MobileClientMessage()
        { 
            
        }

        public void SendByName(String account,List<String> usernames, String senduser, String title, String message)
        {
            String names = "";
            String msgKey = Guid.NewGuid().ToString();
            MobSrvMessageStruct msms = new MobSrvMessageStruct();
            msms.Title = title;
            msms.Note = message;
            msms.SendTime = DateTime.Now;
            msms.SendUser = senduser;
            msms.MapUserCount = usernames.Count;

            foreach(String name in usernames)
            {
                String keyName = account + "." + name;
                if (_userMap.ContainsKey(keyName))
                {
                    _userMap[keyName].Add(msgKey);
                }
                else
                {
                    List<String> tempmsg = new List<String>();
                    tempmsg.Add(msgKey);
                    _userMap.Add(keyName, tempmsg);
                }

                names += keyName + ",";
            }

            _messages.Add(msgKey, msms);

            if (MessageChanged != null)
            {
                MessageChanged.Invoke(_messages.Count, _userMap.Count);
            }
        }

        public List<ClientMessageStruct> GetByName(String account, String username)
        {
            List<ClientMessageStruct> msg = new List<ClientMessageStruct>();
            username = account + "." + username;
            try
            {
                if (_userMap.ContainsKey(username))
                {
                    foreach (String key in _userMap[username])
                    {
                        if (_messages.ContainsKey(key))
                        {
                            ClientMessageStruct cms = new ClientMessageStruct();
                            cms.Title = _messages[key].Title;
                            cms.Note = _messages[key].Note;
                            cms.SendTime = _messages[key].SendTime.ToString("yyyy-MM-dd HH:mm:ss");

                            msg.Add(cms);

                            if (_messages[key].MapUserCount <= 1)
                            {
                                _messages[key] = null;
                                _messages.Remove(key);
                            }
                            else
                            {
                                _messages[key].MapUserCount--;
                            }
                        }
                    }
                    _userMap[username] = null;
                    _userMap.Remove(username);
                }
            }
            catch (Exception) { }

            if (MessageChanged != null)
            {
                MessageChanged.Invoke(_messages.Count, _userMap.Count);
            }

            return msg;
        }
    }

    internal class MobSrvMessageStruct
    {
        public String Title;
        public String Note;
        public String SendUser;
        public DateTime SendTime;
        public int MapUserCount;
    }
}
